package com.xyoto.community.service.impl;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.entity.main.TitleLogo;
import com.xyoto.community.entity.main.TitleName;
import com.xyoto.community.entity.main.UserTitle;
import com.xyoto.community.mapper.main.TitleLogoMapper;
import com.xyoto.community.mapper.main.TitleNameMapper;
import com.xyoto.community.mapper.main.UserTitleMapper;
import com.xyoto.community.po.UserTitlePO;
import com.xyoto.community.service.UserTitleCacheService;
import com.xyoto.community.utils.RedisCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * @author coderFu
 * @create 2021/12/5
 */
@Service
@Slf4j
public class UserTitleCacheServiceImpl implements UserTitleCacheService {

    @Autowired
    private UserTitleMapper userTitleMapper;

    @Autowired
    private TitleNameMapper titleNameMapper;

    @Autowired
    private TitleLogoMapper titleLogoMapper;

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    private final Random random = new Random();


    @Override
    public String getUserTitleNameFromCache(Integer userTitleNameId) {
        //查询缓存中是否存在
        boolean flag = redisCommonUtils.judgeHashExistKey(CacheInstruction.USER_TITLE_NAME, userTitleNameId);
        String titleName = null;
        if (flag) {
            //从缓存中取出
            Object titleNameCacheObj = redisCommonUtils.hget(CacheInstruction.USER_TITLE_NAME, userTitleNameId);
            if (titleNameCacheObj != null) {
                TitleName titleName1 = (TitleName) titleNameCacheObj;
                titleName = titleName1.getTitleName();
            } else {
                return null;
            }
        } else {
            //从数据库中查询
            TitleName titleNameFromDb = titleNameMapper.selectOneById(userTitleNameId);
            //存储到缓存中
            redisCommonUtils.hset(CacheInstruction.USER_TITLE_NAME, userTitleNameId, titleNameFromDb,
                    CacheInstruction.USER_TITLE_EXPIRE_TIME + random.nextInt((int) (CacheInstruction.USER_TITLE_EXPIRE_TIME * 0.05)));
            if (titleNameFromDb == null) {
                //如果数据库中为空
                return null;
            }else {
                titleName = titleNameFromDb.getTitleName();
            }

        }
        return titleName;
    }

    @Override
    public String getUserTitleLogoFromCache(Integer userTitleLogoId) {
        //查询缓存中是否存在
        boolean flag = redisCommonUtils.judgeHashExistKey(CacheInstruction.USER_TITLE_LOGO, userTitleLogoId);
        TitleLogo titleLogoPath = null;
        if (flag) {
            //从缓存中取出
            Object titleLogoCache =redisCommonUtils.hget(CacheInstruction.USER_TITLE_LOGO, userTitleLogoId);
            if (titleLogoCache != null){
                titleLogoPath = (TitleLogo)titleLogoCache;
            }else {
                return null;
            }
        } else {
            //从数据库中查询
            titleLogoPath = titleLogoMapper.selectOneById(userTitleLogoId);
            //存储到缓存中
            redisCommonUtils.hset(CacheInstruction.USER_TITLE_LOGO, userTitleLogoId, titleLogoPath,
                    CacheInstruction.USER_TITLE_EXPIRE_TIME + random.nextInt((int) (CacheInstruction.USER_TITLE_EXPIRE_TIME * 0.05)));
            if (titleLogoPath == null){
                return null;
            }
        }
        return titleLogoPath.getTitleLogoPath() ;
    }

    @Override
    public UserTitlePO getUserTitleFromCache(Integer userTitleNameId, Integer userTitleLogoId) {
        //判断是否为null
        UserTitlePO userTitlePo = null;
        if (userTitleLogoId != null && userTitleNameId != null) {
            userTitlePo = new UserTitlePO();
            userTitlePo.setTitleLogoPath(getUserTitleLogoFromCache(userTitleLogoId));
            userTitlePo.setTitleName(getUserTitleNameFromCache(userTitleNameId));
        } else if (userTitleLogoId != null) {
            userTitlePo = new UserTitlePO();
            userTitlePo.setTitleLogoPath(getUserTitleLogoFromCache(userTitleLogoId));
        } else if (userTitleNameId != null) {
            userTitlePo = new UserTitlePO();
            userTitlePo.setTitleName(getUserTitleNameFromCache(userTitleNameId));
        }
        return userTitlePo;
    }
}
